import turtle
import datetime



#khkjgjgimport turtle
import datetime



#khkjgjg
# 设置背景颜色和透明度
def set_gradient_color(turtle_obj, start_color, end_color, steps):
    delta_r = (end_color[0] - start_color[0]) / steps
    delta_g = (end_color[1] - start_color[1]) / steps
    delta_b = (end_color[2] - start_color[2]) / steps

    for i in range(steps):
        r = start_color[0] + delta_r * i
        g = start_color[1] + delta_g * i
        b = start_color[2] + delta_b * i
        turtle_obj.pencolor(r, g, b)
        yield


# 悬空移动
def move(distance):
    turtle.penup()
    turtle.forward(distance)
    turtle.pendown()


# 创建表针turtle
def createHand(name, length, color):
    turtle.reset()
    turtle.color(color)
    move(-length * 0.01)
    turtle.begin_poly()
    turtle.forward(length * 1.01)
    turtle.end_poly()
    hand = turtle.get_poly()
    turtle.register_shape(name, hand)


# 创建时钟
def createClock(radius):
    turtle.reset()
    turtle.pensize(7)
    gradients = set_gradient_color(turtle, (1, 0, 0), (0, 0, 1), 60)
    for i in range(60):
        move(radius)
        if i % 5 == 0:
            turtle.forward(20)
            move(-radius - 20)
        else:
            turtle.dot(5)
            move(-radius)
        next(gradients)
        turtle.right(6)


# 获得今天是星期几
def getWeekday(today):
    return ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'][today.weekday()]


# 获得今天的日期
def getDate(today):
    return '%s年%s月%s日' % (today.year, today.month, today.day)


# 动态显示表针
def startTick(second_hand, minute_hand, hour_hand, printer):
    today = datetime.datetime.today()
    second = today.second + today.microsecond * 1e-6
    minute = today.minute + second / 60.
    hour = (today.hour + minute / 60) % 12

    second_hand.setheading(6 * second)
    minute_hand.setheading(6 * minute)
    hour_hand.setheading(30 * hour)

    turtle.tracer(False)
    printer.clear()
    printer.goto(0, 65)
    printer.write(getWeekday(today), align='center', font=("Courier New", 16, "bold"))
    printer.goto(0, -85)
    printer.write(getDate(today), align='center', font=("Courier New", 16, "bold"))
    turtle.tracer(True)

    turtle.ontimer(lambda: startTick(second_hand, minute_hand, hour_hand, printer), 100)


# 开始运行时钟
def start():
    turtle.bgcolor("black")
    turtle.tracer(False)
    turtle.mode('logo')
    createHand('second_hand', 150, "red")
    createHand('minute_hand', 125, "white")
    createHand('hour_hand', 85, "yellow")
    second_hand = turtle.Turtle()
    second_hand.shape('second_hand')
    second_hand.shapesize(1, 1, 3)
    second_hand.speed(0)
    second_hand.color("white")

    minute_hand = turtle.Turtle()
    minute_hand.shape('minute_hand')
    minute_hand.shapesize(1, 1, 3)
    minute_hand.speed(0)
    minute_hand.color("white")

    hour_hand = turtle.Turtle()
    hour_hand.shape('hour_hand')
    hour_hand.shapesize(1, 1, 3)
    hour_hand.speed(0)
    hour_hand.color("white")

    printer = turtle.Turtle()
    printer.hideturtle()
    printer.color("cyan")
    printer.penup()

    createClock(160)

    turtle.tracer(True)
    startTick(second_hand, minute_hand, hour_hand, printer)

    # 添加炫目效果
    def blink_hands():
        for hand in [second_hand, minute_hand, hour_hand]:
            hand.color("white")
            turtle.ontimer(lambda: hand.color("red"), 500)
            turtle.ontimer(lambda: hand.color("white"), 1000)

    turtle.ontimer(blink_hands, 2000)

    turtle.mainloop()


if __name__ == '__main__':
    start()
# 设置背景颜色和透明度
def set_gradient_color(turtle_obj, start_color, end_color, steps):
    delta_r = (end_color[0] - start_color[0]) / steps
    delta_g = (end_color[1] - start_color[1]) / steps
    delta_b = (end_color[2] - start_color[2]) / steps

    for i in range(steps):
        r = start_color[0] + delta_r * i
        g = start_color[1] + delta_g * i
        b = start_color[2] + delta_b * i
        turtle_obj.pencolor(r, g, b)
        yield


# 悬空移动
def move(distance):
    turtle.penup()
    turtle.forward(distance)
    turtle.pendown()


# 创建表针turtle
def createHand(name, length, color):
    turtle.reset()
    turtle.color(color)
    move(-length * 0.01)
    turtle.begin_poly()
    turtle.forward(length * 1.01)
    turtle.end_poly()
    hand = turtle.get_poly()
    turtle.register_shape(name, hand)


# 创建时钟
def createClock(radius):
    turtle.reset()
    turtle.pensize(7)
    gradients = set_gradient_color(turtle, (1, 0, 0), (0, 0, 1), 60)
    for i in range(60):
        move(radius)
        if i % 5 == 0:
            turtle.forward(20)
            move(-radius - 20)
        else:
            turtle.dot(5)
            move(-radius)
        next(gradients)
        turtle.right(6)


# 获得今天是星期几
def getWeekday(today):
    return ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'][today.weekday()]


# 获得今天的日期
def getDate(today):
    return '%s年%s月%s日' % (today.year, today.month, today.day)


# 动态显示表针
def startTick(second_hand, minute_hand, hour_hand, printer):
    today = datetime.datetime.today()
    second = today.second + today.microsecond * 1e-6
    minute = today.minute + second / 60.
    hour = (today.hour + minute / 60) % 12

    second_hand.setheading(6 * second)
    minute_hand.setheading(6 * minute)
    hour_hand.setheading(30 * hour)

    turtle.tracer(False)
    printer.clear()
    printer.goto(0, 65)
    printer.write(getWeekday(today), align='center', font=("Courier New", 16, "bold"))
    printer.goto(0, -85)
    printer.write(getDate(today), align='center', font=("Courier New", 16, "bold"))
    turtle.tracer(True)

    turtle.ontimer(lambda: startTick(second_hand, minute_hand, hour_hand, printer), 100)


# 开始运行时钟
def start():
    turtle.bgcolor("black")
    turtle.tracer(False)
    turtle.mode('logo')
    createHand('second_hand', 150, "red")
    createHand('minute_hand', 125, "white")
    createHand('hour_hand', 85, "yellow")
    second_hand = turtle.Turtle()
    second_hand.shape('second_hand')
    second_hand.shapesize(1, 1, 3)
    second_hand.speed(0)
    second_hand.color("white")

    minute_hand = turtle.Turtle()
    minute_hand.shape('minute_hand')
    minute_hand.shapesize(1, 1, 3)
    minute_hand.speed(0)
    minute_hand.color("white")

    hour_hand = turtle.Turtle()
    hour_hand.shape('hour_hand')
    hour_hand.shapesize(1, 1, 3)
    hour_hand.speed(0)
    hour_hand.color("white")

    printer = turtle.Turtle()
    printer.hideturtle()
    printer.color("cyan")
    printer.penup()

    createClock(160)

    turtle.tracer(True)
    startTick(second_hand, minute_hand, hour_hand, printer)

    # 添加炫目效果
    def blink_hands():
        for hand in [second_hand, minute_hand, hour_hand]:
            hand.color("white")
            turtle.ontimer(lambda: hand.color("red"), 500)
            turtle.ontimer(lambda: hand.color("white"), 1000)

    turtle.ontimer(blink_hands, 2000)

    turtle.mainloop()


if __name__ == '__main__':
    start()
